﻿using Microsoft.EntityFrameworkCore;
using S.Initial.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace S.Initial.Infrastructure
{
    public class BaseRepository<T> : IBaseRepository<T> where T : class
    {
        private readonly MyDbcontext db;
        public BaseRepository(MyDbcontext db)
        {
            this.db = db;
        }
        /// <summary>
        /// 添加异步
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public async Task<bool> AddAsync(T t)
        {
            db.Set<T>().AddAsync(t);
            return await db.SaveChangesAsync() > 0;
        }
        /// <summary>
        /// 删除异步
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(int id)
        {
            var list = await db.Set<T>().FindAsync(id);
            db.Set<T>().Remove(list);
            return await db.SaveChangesAsync() > 0;
        }
        /// <summary>
        /// 修改异步
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public async Task<bool> UpdateAsync(T t)
        {
            db.Set<T>().Update(t);
            return await db.SaveChangesAsync() > 0;
        }
        /// <summary>
        /// 显示异步
        /// </summary>
        /// <returns></returns>
        public async Task<List<T>> GetAllAsync()
        {
            return await db.Set<T>().ToListAsync();
        }

    }
}
